package userlogic

import (
	"context"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/login_rpc"

	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/model"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/dao/schema"
	"gitee.com/liuxuezhan/ar-platform/login-rpc/internal/svc"
	config "gitee.com/liuxuezhan/ar-platform/login-rpc/pkg/code"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateAdminLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateAdminLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateAdminLogic {
	return &UpdateAdminLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateAdminLogic) UpdateAdmin(in *login_rpc.UpdateAdminRequest) (*login_rpc.UpdateAdminResponse, error) {
	var commonResp login_rpc.CommonResp
	if in.Id == 0 || in.Account == "" || in.Password == "" || in.Role == 0 || in.Nickname == "" {
		commonResp.Code = config.ParameterError
		commonResp.Message = "account or password is empty"
		return &login_rpc.UpdateAdminResponse{CommonResp: &commonResp}, nil
	}

	var adminUser schema.AdminUser
	adminUser.Account = in.Account
	adminUser.Password = HashPassword(in.Password)
	adminUser.Role = in.Role
	adminUser.Nickname = in.Nickname
	adminUser.Id = in.Id
	adminUser.UserStatus = in.UserStatus
	adminUser.HeadPictureUrl = in.HeadPictureUrl

	err := model.NewAdminUserModel(l.ctx, l.svcCtx.Db).UpdateAdminUserInfo(adminUser)
	if err != nil {
		commonResp.Code = config.DBError
		commonResp.Message = "update failed"
		return &login_rpc.UpdateAdminResponse{CommonResp: &commonResp}, nil
	}
	return &login_rpc.UpdateAdminResponse{CommonResp: &commonResp}, nil
}
